﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Collections.ObjectModel;
namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    class QUAY_Control : IDisposable
    {

        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var list = db.QUAYTN_Get_List().Join(db.NHATHUOC_Get_List(), p => p.MA_NHATHUOC, c => c.MA_NHATHUOC, (p, c) => new { MA_NHATHUOC = p.MA_NHATHUOC, TEN_NHATHUOC = c.TEN_NHATHUOC, MA_QUAY = p.MA_QUAY, TEN_MAY = p.TEN_MAY });

            var typeorder = list.OrderBy(w => w.TEN_NHATHUOC);

            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            QUAYTN_Get_ListResult temp = new QUAYTN_Get_ListResult();
            temp.MA_NHATHUOC = "...";
            temp.MA_QUAY = "...";
            temp.TEN_MAY = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public string loadID_moi( ComboBox cmbNhaThuoc)
        {
            string ID = "";
            var cmbNhaThuocSelect = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;
            int rowcount = db.QUAYTN_GetByMaNhathuoc(cmbNhaThuocSelect.MA_NHATHUOC).Count();
            if (rowcount == 0)
            {
                ID = "Q1";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "Q";
                    for (int j = 0; j < (1 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.QUAYTN_GetByMaNhathuoc(cmbNhaThuocSelect.MA_NHATHUOC);

                    foreach (var t in data)
                    {

                        if (t.MA_QUAY.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }


        public void addData(string ma_quay, ComboBox cmbNhaThuoc, TextBox txtTenMay, RichTextBox txtGhi_Chu)
        {
            if (txtTenMay.Text.Trim() == "" || cmbNhaThuoc.SelectedItem == null)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var nt = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;
                db.QUAYTN_Insert(nt.MA_NHATHUOC, ma_quay, txtTenMay.Text, txtGhi_Chu.Document.Blocks.ToString());
                MessageBox.Show("Đã thêm thành công");
            }
        }


        public void loadData(DataPager dp)
        {
            
            var list = db.QUAYTN_Get_List().Join(db.NHATHUOC_Get_List(), p => p.MA_NHATHUOC, c => c.MA_NHATHUOC, (p, c) => new { MA_NHATHUOC = p.MA_NHATHUOC, TEN_NHATHUOC = c.TEN_NHATHUOC, MA_QUAY = p.MA_QUAY, TEN_MAY = p.TEN_MAY });

            var typeorder = list.OrderBy(w => w.TEN_NHATHUOC);

            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 1;
            foreach (var t in typeorder)
            {
                if (t.TEN_NHATHUOC.Trim() != "")
                {
                    dpSource.Add(new { STT = i, t.TEN_NHATHUOC, t.TEN_MAY, t.MA_QUAY, t.MA_NHATHUOC });
                    i++;
                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
            
        }
        public void loadData(ListView List)
        {
            List.Items.Clear();
            var list = db.QUAYTN_Get_List().Join(db.NHATHUOC_Get_List(), p => p.MA_NHATHUOC, c => c.MA_NHATHUOC, (p, c) => new { MA_NHATHUOC = p.MA_NHATHUOC, TEN_NHATHUOC = c.TEN_NHATHUOC, MA_QUAY = p.MA_QUAY, TEN_MAY = p.TEN_MAY });

            var typeorder = list.OrderBy(w => w.TEN_NHATHUOC);

            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }


        }

        public void updateData(string ID_NT, string id_quay,  ComboBox cmbNhaThuoc, TextBox txtTenMay, RichTextBox txtGhi_Chu)
        {
            if (txtTenMay.Text.Trim() == "" || cmbNhaThuoc.SelectedItem == null)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var nt = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;
                db.QUAYTN_Update(nt.MA_NHATHUOC, ID_NT, id_quay, id_quay, txtTenMay.Text, txtGhi_Chu.Document.Blocks.ToString());
                MessageBox.Show("Đã cập nhật!");
            }
        }

        


        public void deleteData(string ma_nhathuoc, string ma_quay)
        {
            
                if (db.PHIEUXUAT_GetByMaNhathuocMaQuay(ma_nhathuoc, ma_quay).Count() > 0)
                    MessageBox.Show("Quầy này đang được sử dụng, Không thể xóa");
                else
                {
                    db.QUAYTN_Delete(ma_nhathuoc, ma_quay);
                    MessageBox.Show("Xóa thành công!");
                }
            
            
        }
        NHATHUOC_Control nhathuocctrl = new NHATHUOC_Control();
        public void loadForm_Edit(string ma_nhathuoc, string ma_quay, ComboBox cmbNhaThuoc, TextBox txtTenMay, RichTextBox txtGhi_Chu)
        {

            var sl = (QUAYTN_GetByMaNhathuocMaQuayResult)db.QUAYTN_GetByMaNhathuocMaQuay(ma_nhathuoc, ma_quay).Single();


            
            txtTenMay.Text = sl.TEN_MAY;

            nhathuocctrl.loadComboBox(cmbNhaThuoc);
            int t = 0;
            foreach (var j in cmbNhaThuoc.Items)
            {
                var temp = (NHATHUOC_Get_ListResult)j;
                if (temp.MA_NHATHUOC == sl.MA_NHATHUOC)
                {
                    break;
                }
                t++;
            }
            cmbNhaThuoc.SelectedIndex = t;
            txtGhi_Chu.AppendText( sl.GHI_CHU);
        }

        public string[] get_item(ListView list1)
        {
            string[] pro;
            int i = list1.SelectedIndex;
            var list = db.QUAYTN_Get_List().Join(db.NHATHUOC_Get_List(), p => p.MA_NHATHUOC, c => c.MA_NHATHUOC, (p, c) => new { MA_NHATHUOC = p.MA_NHATHUOC, TEN_NHATHUOC = c.TEN_NHATHUOC, MA_QUAY = p.MA_QUAY, TEN_MAY = p.TEN_MAY });
            var typeorder = list.OrderBy(w => w.TEN_NHATHUOC);
            var sl = typeorder.ElementAt(i);
            pro = new string[]{sl.MA_NHATHUOC, sl.MA_QUAY, sl.TEN_MAY, sl.TEN_NHATHUOC};
            
            return pro;

        }
    }

}
